<template>
    <div class="login_container">
        <div class="login_box">
            <!--头像区域-->
            <div class="avatar_box">
                <img class="imglogo" src="../assets/logo.png" alt="">
            </div>
            <!--表单区域
            1. :rules是饿了么的语法 ，不是等于v-bind
            2. ref="loginFormRef"相当于起别名，拿到这个就可以拿到整个的实例对象
            -->
            <el-form ref="loginFormRef" v-bind:model="registerForm" :rules="registerFormRules" label-width="80px" class="login_form">
                <el-form-item prop="username" label="用户名">
                    <el-input v-model="registerForm.username" placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码">
                    <el-input v-model="registerForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" type="password"></el-input>
                </el-form-item>
                <el-form-item prop="remark" label="备注">
                    <el-input v-model="registerForm.remark" placeholder="请输入备注，申请账号" prefix-icon="el-icon-s-promotion"></el-input>
                </el-form-item>
                <!--按钮区域-->
                <el-form-item class="btns">
                    <el-button type="primary" @click="register">注册</el-button>
                    <el-button type="info" @click="backhome">返回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Register",
        data(){
            return{
                registerForm: {
                    "username": "",
                    "password": "",
                    "remark":""
                },
                registerFormRules:{
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        //触发条件：当不在框内，失去焦点的时候
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                    ]
                },
            }
        },
        methods:{
            register(){
                this.$refs.loginFormRef.validate(async valid=>{
                    //打印预验证的值
                    this.$message.success('数据符合规范，提交成功！')
                    //不合适就返回
                    if (!valid) return;
                    //接收表单的值,并发送请求.{data:res} 代表从res中单独把data值拿出来，不要其他的。
                    // const { data: res } = await this.$http.post("force",this.loginForm);
                    const { data: res } = await this.$http.post("/user/register",
                        {
                            "userName":this.registerForm.username,
                            "password":this.registerForm.password,
                            "remark":this.registerForm.remark,
                        });
                    //打印成功消息给用户
                    this.$message.success('注册成功！')
                    console.log(res);
                    //跳转到home页
                    // this.$router.push("/home");
                });
            },
            backhome(){
                this.$router.push("/login");
            }
        }
    }
</script>

<style lang="less" scoped>
    .login_container{
        background-color: #2b4b6b;
        height: 100%;
    }
    .login_box{
        background-color: #fff;
        width: 550px;
        height: 380px;
        border-radius: 30px;
        position: absolute;
        left:50%;
        top:50%;
        transform: translate(-50%,-50%);
    }
    .avatar_box{
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
    }
    .imglogo {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
    .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 10px;
        box-sizing: border-box;
    }
    .btns{
        display: flex;
        justify-content: flex-end;
    }
</style>

